package com.zjx.processor.factory;

import com.zjx.bean.A;
import com.zjx.bean.B;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author zhaojiaxu
 * @date 2021/08/24 10:36
 **/
@Component
public class MybeanFactory implements FactoryBean<Object> , ApplicationContextAware {

	ApplicationContext applicationContext;
	@Override
	public Object getObject() throws Exception {
		//我们通常造一个bean是spring给我们造，但是FactoryBean给了我们一个机会
		//getObject方法，让我们自己定义一个bean，直接返回，spring后面的增强逻辑就直接跳过
		//A@1693  A@1759
		//不会干预
		//每次getObject都是一个新对象
		com.zjx.bean.A a1 =(com.zjx.bean.A) applicationContext.getBean("a");
		//new 对象相当于 分配一块内存，然后引用执行内存
		A a = new A();
		B b = new B();
		a.setB(b);
		b.setA(a);
		//开车，我们给你自动挡了
		//你觉得没感觉，我开手动,你不得自己挂挡么
		return a;

	}

	@Override
	public Class<?> getObjectType() {
		return null;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext=applicationContext;
	}
}

